﻿using Artech.Common;
using Microsoft.Practices.Unity.Utility;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;

namespace Artech.Core.Resources
{
    [DataContract(Namespace = Constants.Namespaces.DataContract)]
    public class StringResourceItem
    {
        [DataMember(Name = "Culture")]
        private string culture;

        [DataMember]
        public string Category { get; private set; }
        [DataMember]
        public string Name { get; private set; }
        [DataMember]
        public string Value { get; private set; }
        public CultureInfo Culture { get; private set; }

        public StringResourceItem(string category, string name, string value, string language, string region = null)
        {
            Guard.ArgumentNotNullOrEmpty(category, "category");
            Guard.ArgumentNotNullOrEmpty(name, "name");
            Guard.ArgumentNotNullOrEmpty(value, "value");
            Guard.ArgumentNotNullOrEmpty(language, "language");

            this.Category = category;
            this.Name = name;
            this.Value = value;
            if (string.IsNullOrEmpty(region))
            {
                this.Culture = new CultureInfo(language);
            }
            else
            {
                this.Culture = new CultureInfo(string.Format("{0}-{1}", language, region));
            }
        }

        [OnSerializing]
        public void OnSerializing(StreamingContext context)
        {
            culture = this.Culture.Name;
        }

        [OnDeserialized]
        public void OnDeserialized(StreamingContext context)
        {
            this.Culture = new CultureInfo(culture);
        }
    }
}